package cn.itcast.n3;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.Test3")
public class Test3 {

    public static void main(String[] args) throws InterruptedException {
        final TwoPhaseTermination tpt = new TwoPhaseTermination();
        tpt.start();

        Thread.sleep(300);
        tpt.stop();
    }
}

@Slf4j(topic = "c.TwoPhaseTermination")
class TwoPhaseTermination{
    private Thread monitor;

    public void start(){
        monitor = new Thread(() -> {
            while (true){
                final Thread current = Thread.currentThread();
                if (current.isInterrupted()) {
                    log.debug("料理后事");
                    break;
                }

                try {
                    Thread.sleep(1000);
                    log.debug("执行监控记录");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    //针对wait类型异常处理，重新设置打断标记，执行料理后事流程
                    current.interrupt();
                    log.debug("重设标记位");
                }
            }
        });

        monitor.start();
    }

    public void stop(){
        monitor.interrupt();
    }
}